/*     */ package system.staros.cblock2;
/*     */ 
/*     */ import java.util.Vector;
/*     */ import system.staros.ablock1.UserUtils;
/*     */ import system.staros.cblock1.Message;
/*     */ import system.staros.cblock1.Scheduler;
/*     */ import system.staros.cblock1.Task;
/*     */ 
 public class Shell extends Task {

 	/**
 	 * Constructor.
 	 */
 	public Shell () {
 		super ();
 	}
 	
 	/**
 	 * Handle messages
 	 */
 	public void handleMessage () {
 		//System.out.print (".");
 		Message msg = getMessage(this.getPID());
 		if ( msg != null ) {
			msg.locked = true;
			switch ( msg.ID ) {
				case Message.MSG_KILL:
					msg.handled = true;
					running = false;
					break;
				default:
					break;
			}
			msg.locked = false;
		} 		
 	}
 	 	
 	/**
 	 * Task body
 	 */
 	public void run () {
 		boolean running = true;
 		boolean req_sent = false;
		byte buffer [];
		int rc;
		boolean fork = false;
		String command;
		String stask;
		String args[] = null;
		Vector args_v;

		print ("Starting Shell. \tPID=");
		println (Integer.toString(this.getPID()));
 				
 		while ( running ) {
 			// Handling messages
 			handleMessage ();

 			print (UserUtils.getUserName()+">");
 			buffer = read ();
 			command = new String (buffer, 0);
 			command = command.trim(); // remove spaces...
 			if ( command.length() != 0 ) {
	 			if ( command.charAt (command.length() - 1) == '&' ) {
 					// Starting as a separate task
 					command = command.substring (0, command.length() - 1);
 					fork = true;
 				} else
 					fork = false;
 					
 				// parsing arguments
 				// TODO:
 				args_v = new Vector();
 				int pos = 0;
 				int prev = 0;
 				while ( pos < command.length() && command.charAt(pos) != ' ' )
 					pos++;
				stask = command.substring (0, pos);
				while ( pos < command.length() && command.charAt(pos) == ' ' )
					pos++;
				prev = pos;
				while ( pos < command.length() ) {
					while ( pos < command.length() && command.charAt(pos) != ' ' )
						pos++;
					String arg = new String (command.substring (prev, pos));
					args_v.add (arg);
					while ( pos < command.length() && command.charAt(pos) == ' ' )
						pos++;
					prev = pos;
				}

				args = new String [args_v.size()];
				for ( int i = 0; i < args_v.size(); i++ )
					args[i] = (String)args_v.elementAt (i);

		 		int task_pid = getScheduler().loadTaskClass ("system/apps/" + stask + ".class");
 				if ( task_pid == -1 ) {
 					println ("Unresolved class <" + stask + ">");				
 				} else {
	 				// Starting new task...
			 		Task task = getScheduler().getTaskByPID (task_pid);
		 			if ( fork ) {
				 		task.setParent (getScheduler().getTaskByPID(0));
		 				task.setPriority (Thread.NORM_PRIORITY);
 						task.start();
			 		} else {
				 		task.setParent (this);
		 				// Just run
		 				for ( int i = 0; i < args.length; i++ )
		 					println (Integer.toString (i) + ": " + args[i]);
		 				task.main ( args );
		 				task.running = false;
		 				
			 		}
 				}
 				command = "";
 			}
 		}
 		
 		println ("Shell stopped.");
 	}
 	
 }